/**Monthly Schedule class
 * 
 * @author locphan
 */

import java.util.ArrayList;
import java.util.Collections;

public class MonthlySchedule
{
   private ArrayList<Request> monthlyschedule;
   
   /**Constructs a monthly schedule
    * 
    */
   public MonthlySchedule()
   {
      monthlyschedule = new ArrayList<Request>();
   }

   /**Adds request to schedule
    * 
    * @param r a request
    * @return true if request is added, false otherwise
    */
   public boolean add(Request r)
   {
      boolean result = false;
      if(this.check(r))
         throw new IllegalArgumentException("Cannot add event. Time conflict occured.");
      else
      {
         monthlyschedule.add(r);
         result = true;
      }
      Collections.sort(monthlyschedule);
      
      
      return result;
   }
   /**Cancels a request
    * 
    * @param name name of the host
    * @return true if request is canceled, false otherwise
    */
   public boolean cancel(String name)
   {
      boolean result = false;
      for(int i=0;i<monthlyschedule.size();i++)
      {
         if(monthlyschedule.get(i).getName().compareToIgnoreCase(name)==0)
         {
            monthlyschedule.remove(i);
            result = true;
         }
      }
      return result;
       
   }
   /**Checks if request can be added
    * 
    * @param r request
    * @return true if time conflict occurs,false otherwise
    */
   public boolean check(Request r)
   {
        for(Request thisrequest: monthlyschedule)
        {
           if(thisrequest.getDate().compareTo(r.getDate())==0)
           {
              if(r.getStartingTime()>thisrequest.getStartingTime())
              {
                 if(thisrequest.getEndTime()>r.getStartingTime())
                    return true;
              }
              else
              {
                 if(r.getEndTime()>thisrequest.getStartingTime())
                    return true;
              }
           }
        }
        return false;
   }
   /**
    * Return a string representation of this monthly schedule
    */
   public String toString()
   {
      String result = "";
      for(int i=0;i<monthlyschedule.size();i++)
      {
         result+= monthlyschedule.get(i).toString() + "\n";
      }
      return result;
   }
}
